package com.example.sunsg.okhttptest.customview;

import android.content.Context;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;
import android.widget.OverScroller;
import android.widget.Scroller;

/**
 * Created by sunsg on 15/12/5.
 */
public class ScrollLinearlayoutView extends LinearLayout{
    private Scroller mScroller;
    private int mTouchSlop;
    private PointF mTouchPoint;
    public ScrollLinearlayoutView(Context context) {
        this(context,null);
    }

    public ScrollLinearlayoutView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context){
        mScroller = new Scroller(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        mTouchPoint = new PointF();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mTouchPoint.set(x,y);
                Log.i("test","onInterceptHoverEvent activon down");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i("test","onInterceptHoverEvent activon move");
                float dy = y - mTouchPoint.y;
                if(Math.abs(dy) > mTouchSlop){
                    return true;//事件不传给子view
                }
                break;
            case MotionEvent.ACTION_UP:

                break;
        }
        return super.onInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mTouchPoint.set(x,y);
                Log.i("test","activon down");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i("test","activon move");
                    float dy = y - mTouchPoint.y;
                    smoothScrollBy(0,(int)(mTouchPoint.y - y)/3);
                    mTouchPoint.y = y;
                break;
            case MotionEvent.ACTION_UP:

                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    //调用此方法滚动到目标位置
    public void smoothScrollTo(int fx, int fy) {
        int dx = fx - mScroller.getFinalX();
        int dy = fy - mScroller.getFinalY();
        smoothScrollBy(dx, dy);
    }

    //调用此方法设置滚动的相对偏移
    public void smoothScrollBy(int dx, int dy) {
        //设置mScroller的滚动偏移量
        Log.i("test","start");
        Log.i("test","dealty = "+dy);
        Log.i("test","mScroller.getFinalY() = "+mScroller.getFinalY());
        mScroller.startScroll(0, mScroller.getFinalY(), 0, dy);
        invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用，否则不一定会刷新界面，看不到滚动效果
    }

    /**缓慢滑动到制定位置*/
    public void smoothScrollDxy(int destx,int dexty){
        int scrollY = getScrollY();
        int delatY = dexty - scrollY;
        mScroller.startScroll(0, scrollY, 0, delatY);
        invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用，否则不一定会刷新界面，看不到滚动效果
    }

    @Override
    public void computeScroll() {
        //先判断mScroller滚动是否完成
        if (mScroller.computeScrollOffset()) {
            //这里调用View的scrollTo()完成实际的滚动
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            //必须调用该方法，否则不一定能看到滚动效果
            postInvalidate();
        }
        super.computeScroll();
    }
}
